package com.ossjk.qlh.minapp.service.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ossjk.qlh.minapp.entity.Orderitm;
import com.ossjk.qlh.minapp.entity.Orders;
import com.ossjk.qlh.minapp.entity.Setmeal;
import com.ossjk.qlh.minapp.mapper.OrdersMapper;
import com.ossjk.qlh.minapp.service.IOrderitmService;
import com.ossjk.qlh.minapp.service.IOrdersService;
import com.ossjk.qlh.minapp.service.ISetmealService;
import com.ossjk.qlh.minapp.vo.OrderSumVo;
import com.ossjk.qlh.minapp.vo.OrdersMenuVo;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单服务实现类
 *
 * @author parker
 * @since 2020-08-14
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersService {

    @Autowired
    private ISetmealService iSetmealService;

    @Autowired
    private IOrderitmService iOrderitmService;

    @Override
    public List<OrderSumVo> orderAllList(QueryWrapper<OrderSumVo> queryWrapper) {
        return baseMapper.orderAllList(queryWrapper);
    }

    @Override
    public List<OrdersMenuVo> queryOrderls(QueryWrapper<OrdersMenuVo> queryWrapper) {
        return baseMapper.queryOrderls(queryWrapper);
    }

    @Override
    public int ordersPay(List ids){
        List<Orders> list = this.listByIds(ids);
        int totals = 0;
        for (Orders o : list) {
            o.setState(2);
            if (this.updateById(o)) {
                totals ++;
            }
        }
        return totals;
    }

    @Override
    public boolean preOrder(Orders orders,List ids) {
        //套餐组
        List<Setmeal> list = iSetmealService.listByIds(ids);

        if (!this.save(orders)) {
            return false;
        }

        List<Orderitm> list1 = new ArrayList();

        //套餐循环
        for (Setmeal setmeal : list) {
            //打印套餐
//            System.out.println(setmeal);
            Orderitm orderitm = new Orderitm();
            //绑定套餐id
            orderitm.setSmid(setmeal.getId());

            List<Map> list2 = (List) JSON.parse(setmeal.getExpression()); //获取一个套餐的表达式
//            System.out.println(list2);
//            【18元】主菜：香菇滑鸡，副菜：青瓜木耳肉片，青菜：芹菜，小食：毛血旺，其它：牛奶6元
            String fnames = orderitm.getFnames();
            fnames = "【" + setmeal.getPrice() + "￥】";
            List list3 = new ArrayList();


            //表达式，以及菜的 id
            for (Map o : list2) {
                //centents层
                List<Map> contents = (List) o.get("contents");

                Map o1 = (Map) contents.get(0);
                String name = (String) o1.get("name");
                String id = (String) o1.get("id");

                if (o.get("id").toString().equals("01")) {
                    orderitm.setMdh(id);
                    fnames += "主食：" + name + ",";
                } else if (o.get("id").toString().equals("02")) {
                    orderitm.setFdh(id);
                    fnames += "副菜：" + name + ",";
                } else if (o.get("id").toString().equals("03")) {
                    orderitm.setVgtb(id);
                    fnames += "青菜：" + name + ",";
                } else if (o.get("id").toString().equals("04")) {
                    orderitm.setMfd(id);
                    fnames += "小食：" + name + ",";
                } else if (o.get("id").toString().equals("05")) {
                    fnames += "其他：";
                    //[{"key":"其它","name":"牛奶","money":6},{"key":"其它","name":"xxx","money":8}]

                    for (Map content : contents) {
                        Map map = new HashMap();
                        String title = (String) content.get("title");
                        String name1 = (String) content.get("name");
                        String substring = title.substring(name1.length(),title.lastIndexOf("￥"));
                        fnames +=name1+",";
                        map.put("key","其他");
                        map.put("name",name);
                        map.put("money",substring.trim());
                        list3.add(map);
                    }
                    //System.out.println(JSON.toJSONString(list3));
                    //其他表达式
                    orderitm.setOtrs(JSON.toJSONString(list3));
                }
            }
//            System.out.println(fnames);
            //订单套餐表达式
            orderitm.setFnames(fnames.substring(0,fnames.lastIndexOf(",")));

            //套餐钱
            orderitm.setCost(setmeal.getPrice());
            //订单id
            orderitm.setOid(orders.getId());

            list1.add(orderitm);
        }
        //批量新增订单套餐
        if (iOrderitmService.saveBatch(list1)) {
            return true;
        }else{
            return false;
        }
    }

}
